/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl.deprecated;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.cx1.model.ds.MockDS;
import org.cx1.model.Place;
import org.cx1.model.utils.SearchFilter;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.ISearchService;

/**
 *
 * @author ECH
 */
public class PlaceSearchService  {

    /*
    @Override
    public ServiceResponse<List<Place>> searchPlaces(String searchTerm) {
        ServiceResponse<List<Place>> response = new ServiceResponse<List<Place>>();
        List<Place> places = new ArrayList<Place>();
        try {
            places = this.search(searchTerm);
            response.setResponse(places);
            response.setSuccessful(true);
        }
        catch (Exception ex) {            
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();      
        }
        return response;
    }
    
    public List<Place> search(String searchTerm) {
        List<Place> response = MockDS.getPlaces();
        List<Place> results = new ArrayList<Place>();
        if (response != null && response.size() > 0) {
            results = search (searchTerm, response);
        }
        return results;
    }

    public List<Place> search(String searchTerm, List<Place> Places) {
        List<Place> results = new ArrayList<Place>();
        
        try {            
            Directory index = new RAMDirectory();
            Analyzer analyzer = new EnglishAnalyzer(Version.LUCENE_36);

            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
            
            IndexWriter indexWriter = new IndexWriter(index, config);
            buildDocuments(indexWriter, Places);
            
            String[] arrFields = {"title"};
            Query q = new MultiFieldQueryParser(Version.LUCENE_36, arrFields, analyzer).parse(searchTerm);

            int hitsPerPage = getItemsLength(Places);
            IndexSearcher searcher = new IndexSearcher(index, true);
            TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
            searcher.search(q, collector);
            ScoreDoc[] hits = collector.topDocs().scoreDocs;

            System.out.println("Found " + hits.length + " hits.");
            for (int i = 0; i < hits.length; ++i) {
                
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);
                System.out.println((i + 1) + ". " + d.get("title") + ". " + d.get("id"));
                
                for (Place record : Places) {
                    if (StringUtils.equalsIgnoreCase(record.getId(), d.get("id"))) {
                        results.add(record);                    
                    }
                } 
            }
            searcher.close();        
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }        
        return results;
    }
    
    private void buildDocuments(IndexWriter writer, List<Place> Places) throws IOException {
        if (Places != null && Places.size() > 0) {
            for (Place project : Places) {
                addDoc(writer, project);
            }
            writer.commit();
        }        
    }
    
    
    private void addDoc(IndexWriter writer, Place Place) throws IOException {
        Document doc = new Document();
        doc.add(new Field("id", Place.getId(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("title", Place.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
                
        writer.addDocument(doc);
    }
    
    private int getItemsLength(List<Place> Place) {
        int length = 0;
        if (Place != null && Place.size() > 0) {
            length = Place.size();
        }
        return length;
    }
    */
}
